console.log(formatDate(new Date(new Date() - 1))) // "right now"
console.log(formatDate(new Date(new Date() - 30 * 1000))) // "30 sec. ago"
console.log(formatDate(new Date(new Date() - 5 * 60 * 1000))) // "5 min. ago"
console.log(formatDate(new Date(new Date() - 86400 * 1000))) // 昨天的日期，例如 31.12.16 20:00

function formatDate(date) {
	// 计算相对时间差
	let diff = new Date() - date

	// 小于 1000 毫秒，表示刚刚发生
	if (diff < 1000) return 'right now'

	let sec = Math.floor(diff / 1000) // 转成秒数

	if (sec < 60) return sec + 'sec. ago'

	let min = Math.floor(sec / 60) // 转成分钟数

	if (min < 60) return min + 'min. ago'

	let d = new Date()
	d = ['0' + d.getDate(), '0' + (d.getMonth() + 1), '' + d.getFullYear(), '0' + d.getHours(), '0' + d.getMinutes()].map(
		(t) => t.slice(-2)
	)

	return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':')
}
